var express = require("express");
var path = require("path");
var logger = require("morgan");
var cookieParse = require("cookie-parser");
var bodyParser = require("body-parser");

let users = require("./routes/users");

var app = express();

app.use(logger("dev"));
app.use(bodyParser.json());
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);
app.use(cookieParse());
app.use(express.static(path.join(__dirname, "public")));
// 使用中间件验证token合法性
// app.use(
//   expressJWT({ secret: "secret" }).unless({
//     path: ["/api/user/token", "/api/admin/register", "/api/admin/login"], // 除了这些地址，其他的url都需要验证
//   })
// );

app.use("/api", users);

app.use(function (req, res, next) {
  var err = new Error("Not Found");
  err.status = 404;
  next(err);
});

app.use(function (err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;
